// $ANTLR 3.3 Nov 30, 2010 12:50:56 /home/ubuntu/kenpu-teaching/201201/csci4020u/04-prog-lang/src/PL.g 2012-03-19 13:17:39

  import helpers.Fun;


import org.antlr.runtime.*;
import java.util.Stack;
import java.util.List;
import java.util.ArrayList;

public class PLParser extends Parser {
    public static final String[] tokenNames = new String[] {
        "<invalid>", "<EOR>", "<DOWN>", "<UP>", "NUM", "PLUS", "SUB", "MULT", "DIV", "WS", "'('", "')'"
    };
    public static final int EOF=-1;
    public static final int T__10=10;
    public static final int T__11=11;
    public static final int NUM=4;
    public static final int PLUS=5;
    public static final int SUB=6;
    public static final int MULT=7;
    public static final int DIV=8;
    public static final int WS=9;

    // delegates
    // delegators


        public PLParser(TokenStream input) {
            this(input, new RecognizerSharedState());
        }
        public PLParser(TokenStream input, RecognizerSharedState state) {
            super(input, state);
             
        }
        

    public String[] getTokenNames() { return PLParser.tokenNames; }
    public String getGrammarFileName() { return "/home/ubuntu/kenpu-teaching/201201/csci4020u/04-prog-lang/src/PL.g"; }



    // $ANTLR start "start"
    // /home/ubuntu/kenpu-teaching/201201/csci4020u/04-prog-lang/src/PL.g:6:1: start : e= expr ;
    public final void start() throws RecognitionException {
        Integer e = null;


        try {
            // /home/ubuntu/kenpu-teaching/201201/csci4020u/04-prog-lang/src/PL.g:6:7: (e= expr )
            // /home/ubuntu/kenpu-teaching/201201/csci4020u/04-prog-lang/src/PL.g:6:9: e= expr
            {
            pushFollow(FOLLOW_expr_in_start17);
            e=expr();

            state._fsp--;

            System.out.println("Ans:" + e);

            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return ;
    }
    // $ANTLR end "start"


    // $ANTLR start "expr"
    // /home/ubuntu/kenpu-teaching/201201/csci4020u/04-prog-lang/src/PL.g:8:1: expr returns [Integer value] : ( NUM | '(' f= fun (e= expr )* ')' );
    public final Integer expr() throws RecognitionException {
        Integer value = null;

        Token NUM1=null;
        int f = 0;

        Integer e = null;



          value = null;

        try {
            // /home/ubuntu/kenpu-teaching/201201/csci4020u/04-prog-lang/src/PL.g:12:3: ( NUM | '(' f= fun (e= expr )* ')' )
            int alt2=2;
            int LA2_0 = input.LA(1);

            if ( (LA2_0==NUM) ) {
                alt2=1;
            }
            else if ( (LA2_0==10) ) {
                alt2=2;
            }
            else {
                NoViableAltException nvae =
                    new NoViableAltException("", 2, 0, input);

                throw nvae;
            }
            switch (alt2) {
                case 1 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/04-prog-lang/src/PL.g:12:5: NUM
                    {
                    NUM1=(Token)match(input,NUM,FOLLOW_NUM_in_expr38); 
                     value = Integer.parseInt((NUM1!=null?NUM1.getText():null)); 

                    }
                    break;
                case 2 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/04-prog-lang/src/PL.g:13:5: '(' f= fun (e= expr )* ')'
                    {
                    match(input,10,FOLLOW_10_in_expr46); 
                    pushFollow(FOLLOW_fun_in_expr50);
                    f=fun();

                    state._fsp--;

                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/04-prog-lang/src/PL.g:13:15: (e= expr )*
                    loop1:
                    do {
                        int alt1=2;
                        int LA1_0 = input.LA(1);

                        if ( (LA1_0==NUM||LA1_0==10) ) {
                            alt1=1;
                        }


                        switch (alt1) {
                    	case 1 :
                    	    // /home/ubuntu/kenpu-teaching/201201/csci4020u/04-prog-lang/src/PL.g:13:16: e= expr
                    	    {
                    	    pushFollow(FOLLOW_expr_in_expr55);
                    	    e=expr();

                    	    state._fsp--;

                    	    value = Fun.eval(f, value, e);

                    	    }
                    	    break;

                    	default :
                    	    break loop1;
                        }
                    } while (true);

                    match(input,11,FOLLOW_11_in_expr61); 

                    }
                    break;

            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return value;
    }
    // $ANTLR end "expr"


    // $ANTLR start "fun"
    // /home/ubuntu/kenpu-teaching/201201/csci4020u/04-prog-lang/src/PL.g:16:1: fun returns [int funcode] : ( PLUS | SUB | MULT | DIV );
    public final int fun() throws RecognitionException {
        int funcode = 0;

        try {
            // /home/ubuntu/kenpu-teaching/201201/csci4020u/04-prog-lang/src/PL.g:17:3: ( PLUS | SUB | MULT | DIV )
            int alt3=4;
            switch ( input.LA(1) ) {
            case PLUS:
                {
                alt3=1;
                }
                break;
            case SUB:
                {
                alt3=2;
                }
                break;
            case MULT:
                {
                alt3=3;
                }
                break;
            case DIV:
                {
                alt3=4;
                }
                break;
            default:
                NoViableAltException nvae =
                    new NoViableAltException("", 3, 0, input);

                throw nvae;
            }

            switch (alt3) {
                case 1 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/04-prog-lang/src/PL.g:17:5: PLUS
                    {
                    match(input,PLUS,FOLLOW_PLUS_in_fun78); 
                    funcode = 1;

                    }
                    break;
                case 2 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/04-prog-lang/src/PL.g:18:5: SUB
                    {
                    match(input,SUB,FOLLOW_SUB_in_fun86); 
                    funcode = 2;

                    }
                    break;
                case 3 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/04-prog-lang/src/PL.g:19:5: MULT
                    {
                    match(input,MULT,FOLLOW_MULT_in_fun95); 
                    funcode = 3;

                    }
                    break;
                case 4 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/04-prog-lang/src/PL.g:20:5: DIV
                    {
                    match(input,DIV,FOLLOW_DIV_in_fun103); 
                    funcode = 4;

                    }
                    break;

            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return funcode;
    }
    // $ANTLR end "fun"

    // Delegated rules


 

    public static final BitSet FOLLOW_expr_in_start17 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_NUM_in_expr38 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_10_in_expr46 = new BitSet(new long[]{0x00000000000001E0L});
    public static final BitSet FOLLOW_fun_in_expr50 = new BitSet(new long[]{0x0000000000000C10L});
    public static final BitSet FOLLOW_expr_in_expr55 = new BitSet(new long[]{0x0000000000000C10L});
    public static final BitSet FOLLOW_11_in_expr61 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_PLUS_in_fun78 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_SUB_in_fun86 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_MULT_in_fun95 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_DIV_in_fun103 = new BitSet(new long[]{0x0000000000000002L});

}